<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="教师名称"></el-input>
    </el-form-item>
    <el-form-item label="教师头像" prop="img">
      <el-input v-model="dataForm.img" placeholder="教师头像"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="籍贯" prop="nativePlace">
      <el-input v-model="dataForm.nativePlace" placeholder="籍贯"></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="出生日期"></el-input>
    </el-form-item>
    <el-form-item label="参加工作时间" prop="workTime">
      <el-input v-model="dataForm.workTime" placeholder="参加工作时间"></el-input>
    </el-form-item>
    <el-form-item label="现从事专业" prop="profession">
      <el-input v-model="dataForm.profession" placeholder="现从事专业"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          img: '',
          sex: '',
          nativePlace: '',
          birthday: '',
          workTime: '',
          profession: ''
        },
        dataRule: {
          name: [
            { required: true, message: '教师名称不能为空', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '教师头像不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          nativePlace: [
            { required: true, message: '籍贯不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          workTime: [
            { required: true, message: '参加工作时间不能为空', trigger: 'blur' }
          ],
          profession: [
            { required: true, message: '现从事专业不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teacherinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.teacherInfo.name
                this.dataForm.img = data.teacherInfo.img
                this.dataForm.sex = data.teacherInfo.sex
                this.dataForm.nativePlace = data.teacherInfo.nativePlace
                this.dataForm.birthday = data.teacherInfo.birthday
                this.dataForm.workTime = data.teacherInfo.workTime
                this.dataForm.profession = data.teacherInfo.profession
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teacherinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'img': this.dataForm.img,
                'sex': this.dataForm.sex,
                'nativePlace': this.dataForm.nativePlace,
                'birthday': this.dataForm.birthday,
                'workTime': this.dataForm.workTime,
                'profession': this.dataForm.profession
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
